package utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class EmailSender {
    private static final String USERNAME = "alkop3011@gmail.com";
    private static final String PASSWORD = "2yoryS5i";
    private static final String PROPERTIES_PATH = "mail.properties";

    public static void sendEmail(String toEmail, String subject, String emailMessage) throws Exception {

        Properties properties = new Properties();
        InputStream inputStream = new FileInputStream(PROPERTIES_PATH);
        properties.load(inputStream);

        Session session = Session.getInstance(properties, new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(USERNAME, PASSWORD);
            }
        });

        Message message = new MimeMessage(session);
        //от кого
        message.setFrom(new InternetAddress(USERNAME));
        //кому
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail));
        //Заголовок письма
        message.setSubject(subject);
        //Содержимое
        message.setText(emailMessage);
        //Отправляем сообщение
        Transport.send(message);

    }


}
